<!DOCTYPE html>



  


<html class="theme-next gemini use-motion" lang="zh-Hans">
<head><meta name="generator" content="Hexo 3.8.0">
  <meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1">
<meta name="theme-color" content="#222">









<meta http-equiv="Cache-Control" content="no-transform">
<meta http-equiv="Cache-Control" content="no-siteapp">
















  
  
  <link href="../../../../lib/fancybox/source/jquery.fancybox.css?v=2.1.5" rel="stylesheet" type="text/css">




  
  
  
  

  
    
    
  

  

  

  

  

  
    
    
    <link href="//fonts.useso.com/css?family=Lato:300,300italic,400,400italic,700,700italic&subset=latin,latin-ext" rel="stylesheet" type="text/css">
  






<link href="../../../../lib/font-awesome/css/font-awesome.min.css?v=4.6.2" rel="stylesheet" type="text/css">

<link href="../../../../css/main.css?v=5.1.4" rel="stylesheet" type="text/css">


  <link rel="apple-touch-icon" sizes="180x180" href="../../../../images/apple-touch-icon-next.png?v=5.1.4">


  <link rel="icon" type="image/png" sizes="32x32" href="../../../../images/favicon-32x32-next.png?v=5.1.4">


  <link rel="icon" type="image/png" sizes="16x16" href="../../../../images/favicon-16x16-next.png?v=5.1.4">


  <link rel="mask-icon" href="../../../../images/logo.svg?v=5.1.4" color="#222">





  <meta name="keywords" content="zabbix,grafana,">










<meta name="description" content="一，利用zabbix自带模板监控mysql状态： 1，在从的mysql服务器上面创建一个用于zabbix监控的用户 grant replication client on . to zabbix@’localhost’  IDENTIFIED BY ‘PASSWORD’; 2，根据zabbix监控mysql的key改写脚本 #/bin/bashDEF=”–defaults-file=/etc/za">
<meta name="keywords" content="zabbix,grafana">
<meta property="og:type" content="article">
<meta property="og:title" content="Zabbix 监控Mysql状态以及mysql主从">
<meta property="og:url" content="http://demo_demo.gitee.io/2015/10/23/zabbix-e7-9b-91-e6-8e-a7mysql-e7-8a-b6-e6-80-81-e4-bb-a5-e5-8f-8amysql-e4-b8-bb-e4-bb-8e/index.html">
<meta property="og:site_name" content="运维人">
<meta property="og:description" content="一，利用zabbix自带模板监控mysql状态： 1，在从的mysql服务器上面创建一个用于zabbix监控的用户 grant replication client on . to zabbix@’localhost’  IDENTIFIED BY ‘PASSWORD’; 2，根据zabbix监控mysql的key改写脚本 #/bin/bashDEF=”–defaults-file=/etc/za">
<meta property="og:locale" content="zh-Hans">
<meta property="og:image" content="https://raw.githubusercontent.com/guomaoqiu/myblog_backup/master/uploads/2015/10/Screenshot-from-2015-10-23-131307.png">
<meta property="og:image" content="https://raw.githubusercontent.com/guomaoqiu/myblog_backup/master/uploads/2015/10/Screenshot-from-2015-10-23-131457.png">
<meta property="og:image" content="https://raw.githubusercontent.com/guomaoqiu/myblog_backup/master/uploads/2015/10/Screenshot-from-2015-10-23-132702.png">
<meta property="og:image" content="https://raw.githubusercontent.com/guomaoqiu/myblog_backup/master/uploads/2015/10/Screenshot-from-2015-10-23-132949.png">
<meta property="og:image" content="https://raw.githubusercontent.com/guomaoqiu/myblog_backup/master/uploads/2015/10/Screenshot-from-2015-10-23-133031.png">
<meta property="og:image" content="https://raw.githubusercontent.com/guomaoqiu/myblog_backup/master/uploads/2015/10/Screenshot-from-2015-10-23-134737.png">
<meta property="og:image" content="https://raw.githubusercontent.com/guomaoqiu/myblog_backup/master/uploads/2015/10/Screenshot-from-2015-10-23-140321.png">
<meta property="og:image" content="https://raw.githubusercontent.com/guomaoqiu/myblog_backup/master/uploads/2015/10/Screenshot-from-2015-10-23-140754.png">
<meta property="og:image" content="https://raw.githubusercontent.com/guomaoqiu/myblog_backup/master/uploads/2015/10/Screenshot-from-2015-10-23-141313.png">
<meta property="og:image" content="https://raw.githubusercontent.com/guomaoqiu/myblog_backup/master/uploads/2015/10/Screenshot-from-2015-10-23-141153.png">
<meta property="og:image" content="https://raw.githubusercontent.com/guomaoqiu/myblog_backup/master/uploads/2015/10/Screenshot-from-2015-10-23-141621.png">
<meta property="og:image" content="https://raw.githubusercontent.com/guomaoqiu/myblog_backup/master/uploads/2015/10/Screenshot-from-2015-10-23-141903.png">
<meta property="og:image" content="https://raw.githubusercontent.com/guomaoqiu/myblog_backup/master/uploads/2015/10/Screenshot-from-2015-10-23-141935.png">
<meta property="og:image" content="https://raw.githubusercontent.com/guomaoqiu/myblog_backup/master/uploads/2015/10/Screenshot-from-2015-10-23-142528.png">
<meta property="og:image" content="https://raw.githubusercontent.com/guomaoqiu/myblog_backup/master/uploads/2015/10/Screenshot-from-2015-10-23-142832.png">
<meta property="og:image" content="https://raw.githubusercontent.com/guomaoqiu/myblog_backup/master/uploads/2015/10/zabbix_1.png">
<meta property="og:image" content="https://raw.githubusercontent.com/guomaoqiu/myblog_backup/master/uploads/2015/10/zabbix_2.png">
<meta property="og:image" content="https://raw.githubusercontent.com/guomaoqiu/myblog_backup/master/uploads/2015/10/Screenshot-from-2015-10-23-144225.png">
<meta property="og:image" content="https://raw.githubusercontent.com/guomaoqiu/myblog_backup/master/uploads/2015/10/Screenshot-from-2015-10-23-144008.png">
<meta property="og:updated_time" content="2018-12-10T10:28:44.000Z">
<meta name="twitter:card" content="summary">
<meta name="twitter:title" content="Zabbix 监控Mysql状态以及mysql主从">
<meta name="twitter:description" content="一，利用zabbix自带模板监控mysql状态： 1，在从的mysql服务器上面创建一个用于zabbix监控的用户 grant replication client on . to zabbix@’localhost’  IDENTIFIED BY ‘PASSWORD’; 2，根据zabbix监控mysql的key改写脚本 #/bin/bashDEF=”–defaults-file=/etc/za">
<meta name="twitter:image" content="https://raw.githubusercontent.com/guomaoqiu/myblog_backup/master/uploads/2015/10/Screenshot-from-2015-10-23-131307.png">



<script type="text/javascript" id="hexo.configurations">
  var NexT = window.NexT || {};
  var CONFIG = {
    root: '/',
    scheme: 'Gemini',
    version: '5.1.4',
    sidebar: {"position":"left","display":"post","offset":12,"b2t":false,"scrollpercent":false,"onmobile":false},
    fancybox: true,
    tabs: true,
    motion: {"enable":true,"async":false,"transition":{"post_block":"fadeIn","post_header":"slideDownIn","post_body":"slideDownIn","coll_header":"slideLeftIn","sidebar":"slideUpIn"}},
    duoshuo: {
      userId: '0',
      author: '博主'
    },
    algolia: {
      applicationID: '',
      apiKey: '',
      indexName: '',
      hits: {"per_page":10},
      labels: {"input_placeholder":"Search for Posts","hits_empty":"We didn't find any results for the search: ${query}","hits_stats":"${hits} results found in ${time} ms"}
    }
  };
</script>



  <link rel="canonical" href="http://demo_demo.gitee.io/2015/10/23/zabbix-e7-9b-91-e6-8e-a7mysql-e7-8a-b6-e6-80-81-e4-bb-a5-e5-8f-8amysql-e4-b8-bb-e4-bb-8e/">





  <title>Zabbix 监控Mysql状态以及mysql主从 | 运维人</title>
  








</head>

<body itemscope="" itemtype="http://schema.org/WebPage" lang="zh-Hans">

  
  
    
  

  <div class="container sidebar-position-left page-post-detail">
    <div class="headband"></div>
<a href="https://github.com/guomaoqiu" class="github-corner" aria-label="View source on GitHub"><svg width="80" height="80" viewbox="0 0 250 250" style="fill:#FD6C6C; color:#fff; position: absolute; top: 0; border: 0; right: 0;" aria-hidden="true"><path d="M0,0 L115,115 L130,115 L142,142 L250,250 L250,0 Z"/><path d="M128.3,109.0 C113.8,99.7 119.0,89.6 119.0,89.6 C122.0,82.7 120.5,78.6 120.5,78.6 C119.2,72.0 123.4,76.3 123.4,76.3 C127.3,80.9 125.5,87.3 125.5,87.3 C122.9,97.6 130.6,101.9 134.4,103.2" fill="currentColor" style="transform-origin: 130px 106px;" class="octo-arm"/><path d="M115.0,115.0 C114.9,115.1 118.7,116.5 119.8,115.4 L133.7,101.6 C136.9,99.2 139.9,98.4 142.2,98.6 C133.8,88.0 127.5,74.4 143.8,58.0 C148.5,53.4 154.0,51.2 159.7,51.0 C160.3,49.4 163.2,43.6 171.4,40.1 C171.4,40.1 176.1,42.5 178.8,56.2 C183.1,58.6 187.2,61.8 190.9,65.4 C194.5,69.0 197.7,73.2 200.1,77.6 C213.8,80.2 216.3,84.9 216.3,84.9 C212.7,93.1 206.9,96.0 205.4,96.6 C205.1,102.4 203.0,107.8 198.3,112.5 C181.9,128.9 168.3,122.5 157.7,114.1 C157.9,116.9 156.7,120.9 152.7,124.9 L141.0,136.5 C139.8,137.7 141.6,141.9 141.8,141.8 Z" fill="currentColor" class="octo-body"/></svg></a><style>.github-corner:hover .octo-arm{animation:octocat-wave 560ms ease-in-out}@keyframes octocat-wave{0%,100%{transform:rotate(0)}20%,60%{transform:rotate(-25deg)}40%,80%{transform:rotate(10deg)}}@media (max-width:500px){.github-corner:hover .octo-arm{animation:none}.github-corner .octo-arm{animation:octocat-wave 560ms ease-in-out}}</style>
    <header id="header" class="header" itemscope="" itemtype="http://schema.org/WPHeader">
      <div class="header-inner"><div class="site-brand-wrapper">
  <div class="site-meta ">
    

    <div class="custom-logo-site-title">
      <a href="/" class="brand" rel="start">
        <span class="logo-line-before"><i></i></span>
        <span class="site-title">运维人</span>
        <span class="logo-line-after"><i></i></span>
      </a>
    </div>
      
        <p class="site-subtitle">生命在于折腾</p>
      
  </div>

  <div class="site-nav-toggle">
    <button>
      <span class="btn-bar"></span>
      <span class="btn-bar"></span>
      <span class="btn-bar"></span>
    </button>
  </div>
</div>

<nav class="site-nav">
  

  
    <ul id="menu" class="menu">
      
        
        <li class="menu-item menu-item-home">
          <a href="" rel="section">
            
              <i class="menu-item-icon fa fa-fw fa-home"></i> <br>
            
            首页
          </a>
        </li>
      
        
        <li class="menu-item menu-item-about">
          <a href="" rel="section">
            
              <i class="menu-item-icon fa fa-fw fa-user"></i> <br>
            
            关于
          </a>
        </li>
      
        
        <li class="menu-item menu-item-tags">
          <a href="" rel="section">
            
              <i class="menu-item-icon fa fa-fw fa-tags"></i> <br>
            
            标签
          </a>
        </li>
      
        
        <li class="menu-item menu-item-categories">
          <a href="" rel="section">
            
              <i class="menu-item-icon fa fa-fw fa-th"></i> <br>
            
            分类
          </a>
        </li>
      
        
        <li class="menu-item menu-item-archives">
          <a href="" rel="section">
            
              <i class="menu-item-icon fa fa-fw fa-archive"></i> <br>
            
            归档
          </a>
        </li>
      

      
    </ul>
  

  
</nav>



 </div>
    </header>

    <main id="main" class="main">
      <div class="main-inner">
        <div class="content-wrap">
          <div id="content" class="content">
            

  <div id="posts" class="posts-expand">
    

  

  
  
  

  <article class="post post-type-normal" itemscope="" itemtype="http://schema.org/Article">
  
  
  
  <div class="post-block">
    <link itemprop="mainEntityOfPage" href="http://demo_demo.gitee.io">

    <span hidden itemprop="author" itemscope="" itemtype="http://schema.org/Person">
      <meta itemprop="name" content="OutMan">
      <meta itemprop="description" content="">
      <meta itemprop="image" content="https://raw.githubusercontent.com/guomaoqiu/myblog_backup/master/uploads/myphoto.png">
    </span>

    <span hidden itemprop="publisher" itemscope="" itemtype="http://schema.org/Organization">
      <meta itemprop="name" content="运维人">
    </span>

    
      <header class="post-header">

        
        
          <h1 class="post-title" itemprop="name headline">Zabbix 监控Mysql状态以及mysql主从</h1>
        

        <div class="post-meta">
          <span class="post-time">
            
              <span class="post-meta-item-icon">
                <i class="fa fa-calendar-o"></i>
              </span>
              
                <span class="post-meta-item-text">发表于</span>
              
              <time title="创建于" itemprop="dateCreated datePublished" datetime="2015-10-23T14:48:07+08:00">
                2015-10-23
              </time>
            

            

            
          </span>

          
            <span class="post-category">
            
              <span class="post-meta-divider">|</span>
            
              <span class="post-meta-item-icon">
                <i class="fa fa-folder-o"></i>
              </span>
              
                <span class="post-meta-item-text">分类于</span>
              
              
                <span itemprop="about" itemscope="" itemtype="http://schema.org/Thing">
                  <a href="../../../../categories/Monitor/" itemprop="url" rel="index">
                    <span itemprop="name">Monitor</span>
                  </a>
                </span>

                
                
              
            </span>
          

          
            
          

          
          

          

          

          

        </div>
      </header>
    

    
    
    
    <div class="post-body" itemprop="articleBody">

      
      

      
        <p>一，利用zabbix自带模板监控mysql状态： 1，在从的mysql服务器上面创建一个用于zabbix监控的用户 grant replication client on <em>.</em> to zabbix@’localhost’  IDENTIFIED BY ‘PASSWORD’; 2，根据zabbix监控mysql的key改写脚本</p>
<p>#/bin/bash<br>DEF=”–defaults-file=/etc/zabbix/my.conf”<br>MYSQL=’/usr/local/webservers/mysql-5.6.19/bin/mysqladmin’<br>ARGS=1<br>if [ $# -ne “$ARGS” ];then<br>    echo “Please input one arguement:”<br>fi<br>case $1 in<br>    Uptime)<br>        result=<code>${MYSQL} $DEF status|cut -f2 -d&quot;:&quot;|cut -f1 -d&quot;T&quot;</code><br>            echo $result<br>            ;;<br>        Com_update)<br>            result=<code>${MYSQL} $DEF extended-status |grep -w &quot;Com_update&quot;|cut -d&quot;|&quot; -f3</code><br>            echo $result<br>            ;;<br>        Slow_queries)<br>        result=<code>${MYSQL} $DEF status |cut -f5 -d&quot;:&quot;|cut -f1 -d&quot;O&quot;</code><br>                echo $result<br>                ;;<br>    Com_select)<br>        result=<code>${MYSQL} $DEF extended-status |grep -w &quot;Com_select&quot;|cut -d&quot;|&quot; -f3</code><br>                echo $result<br>                ;;<br>    Com_rollback)<br>        result=<code>${MYSQL} $DEF extended-status |grep -w &quot;Com_rollback&quot;|cut -d&quot;|&quot; -f3</code><br>                echo $result<br>                ;;<br>    Questions)<br>        result=<code>${MYSQL} $DEF status|cut -f4 -d&quot;:&quot;|cut -f1 -d&quot;S&quot;</code><br>                echo $result<br>                ;;<br>    Com_insert)<br>        result=<code>${MYSQL} $DEF extended-status |grep -w &quot;Com_insert&quot;|cut -d&quot;|&quot; -f3</code><br>                echo $result<br>                ;;<br>    Com_delete)<br>        result=<code>${MYSQL} $DEF extended-status |grep -w &quot;Com_delete&quot;|cut -d&quot;|&quot; -f3</code><br>                echo $result<br>                ;;<br>    Com_commit)<br>        result=<code>${MYSQL} $DEF extended-status |grep -w &quot;Com_commit&quot;|cut -d&quot;|&quot; -f3</code><br>                echo $result<br>                ;;<br>    Bytes_sent)<br>        result=<code>${MYSQL} $DEF extended-status |grep -w &quot;Bytes_sent&quot; |cut -d&quot;|&quot; -f3</code><br>                echo $result<br>                ;;<br>    Bytes_received)<br>        result=<code>${MYSQL} $DEF extended-status |grep -w &quot;Bytes_received&quot; |cut -d&quot;|&quot; -f3</code><br>                echo $result<br>                ;;<br>    Com_begin)<br>        result=<code>${MYSQL} $DEF extended-status |grep -w &quot;Com_begin&quot;|cut -d&quot;|&quot; -f3</code><br>                echo $result<br>                ;; </p>
<pre><code>*) 
echo &quot;Usage:$0(Uptime|Com\_update|Slow\_queries|Com\_select|Com\_rollback|Questions)&quot; 
;; 
</code></pre><p>esac</p>
<p>上面的脚本中： /etc/zabbix/my.conf 这个文件定义的是zabbix这个mysql用户的信息，然后直接指定这个文件，如果我们在命令行直接输入zabbix的用户密码的话会一行提示，影响我们zabbix取值，例如： <a href="https://raw.githubusercontent.com/guomaoqiu/myblog_backup/master/uploads/2015/10/Screenshot-from-2015-10-23-131307.png" target="_blank" rel="noopener"><img src="https://raw.githubusercontent.com/guomaoqiu/myblog_backup/master/uploads/2015/10/Screenshot-from-2015-10-23-131307.png" alt="Screenshot from 2015-10-23 13:13:07"></a> 如果我们直接指定了这个文件，那么那行提示就不会出现 <a href="https://raw.githubusercontent.com/guomaoqiu/myblog_backup/master/uploads/2015/10/Screenshot-from-2015-10-23-131457.png" target="_blank" rel="noopener"><img src="https://raw.githubusercontent.com/guomaoqiu/myblog_backup/master/uploads/2015/10/Screenshot-from-2015-10-23-131457.png" alt="Screenshot from 2015-10-23 13:14:57"></a> /etc/zabbix/my.conf的内容：</p>
<p>[client]<br>host=localhost<br>user=zabbix<br>password=’PASSWORD’<br>socket = /data/mysql/mysql.sock</p>
<p>3，设置zabbix_agent端 首先启用自定义的key 去掉zabbix_agent配置文件的259行: Include=/usr/local/etc/zabbix_agentd.conf.d/*.conf 然后自定义一个mysql-status.conf 在这个目录下，内容为：</p>
<p>UserParameter=mysql.version,/usr/local/webservers/mysql-5.6.19/bin/mysql -V<br>UserParameter=mysql.ping,/usr/local/webservers/mysql-5.6.19/bin/mysqladmin –defaults-file=/etc/zabbix/my.conf ping | grep -c alive<br>UserParameter=mysql.status[*],/home/shell/mysql-status.sh $1</p>
<p>#注意：这里自定义的key 建议使用命令的绝对路径</p>
<p>ok 上面的内容定义好之后重启zabbix_agent服务，然后就可以在zabbix_server端看一下能不能获取到key值 <a href="https://raw.githubusercontent.com/guomaoqiu/myblog_backup/master/uploads/2015/10/Screenshot-from-2015-10-23-132702.png" target="_blank" rel="noopener"><img src="https://raw.githubusercontent.com/guomaoqiu/myblog_backup/master/uploads/2015/10/Screenshot-from-2015-10-23-132702.png" alt="Screenshot from 2015-10-23 13:27:02"></a> 好啦，以上内容中可以看到取值正常，我们在zabbix dashboard中查看一下吧； <a href="https://raw.githubusercontent.com/guomaoqiu/myblog_backup/master/uploads/2015/10/Screenshot-from-2015-10-23-132949.png" target="_blank" rel="noopener"><img src="https://raw.githubusercontent.com/guomaoqiu/myblog_backup/master/uploads/2015/10/Screenshot-from-2015-10-23-132949.png" alt="Screenshot from 2015-10-23 13:29:49"></a> <a href="https://raw.githubusercontent.com/guomaoqiu/myblog_backup/master/uploads/2015/10/Screenshot-from-2015-10-23-133031.png" target="_blank" rel="noopener"><img src="https://raw.githubusercontent.com/guomaoqiu/myblog_backup/master/uploads/2015/10/Screenshot-from-2015-10-23-133031.png" alt="Screenshot from 2015-10-23 13:30:31"></a> 至此已经监控到mysql的状态啦；这里一定要注意mysql用户取获取mysql服务的状态时候的权限； 二、zabbix监控mysql主从状态 跟上面的步骤差不多，这里使用的mysql用户我还是使用的zabbix用户 那这里直接就自定义key 啦，大家都知道mysql主从状态我们一般通过在mysql slaves上面的show slave status 然后看</p>
<p>Slave_IO_Running: Yes<br>Slave_SQL_Running: Yes</p>
<p>这两个值，如果其中一个不为Yes那说明主从同步是有问题的，那我们用zabbix这个的身份来获取这个值 编写脚本：mysql_replication.sh</p>
<p>#!/bin/bash<br>/usr/local/webservers/mysql-5.6.19/bin/mysql –defaults-file=/etc/zabbix/my.conf -e ‘show slave status\G’ | grep -E “Slave_IO_Running:|Slave_SQL_Running:”  | awk ‘{print $2}’ | grep -c Yes</p>
<p>这个脚本的用途是，用zabbix用户身份执行”show slave status\G”这个命令，然后截取Yes这个关键字的行数是2 或者非 2 <a href="https://raw.githubusercontent.com/guomaoqiu/myblog_backup/master/uploads/2015/10/Screenshot-from-2015-10-23-134737.png" target="_blank" rel="noopener"><img src="https://raw.githubusercontent.com/guomaoqiu/myblog_backup/master/uploads/2015/10/Screenshot-from-2015-10-23-134737.png" alt="Screenshot from 2015-10-23 13:47:37"></a> 在zabbix_server 端/usr/local/etc/zabbix_agentd.conf.d/目录下新建文件：mysql-replication.conf，内容为：</p>
<p>UserParameter=mysql.replication,/home/shell/mysql-replication.sh</p>
<p>写好之后，重启zabbix_agent 然后在zabbix_server端测试一下看能否获取到这个值 <a href="https://raw.githubusercontent.com/guomaoqiu/myblog_backup/master/uploads/2015/10/Screenshot-from-2015-10-23-140321.png" target="_blank" rel="noopener"><img src="https://raw.githubusercontent.com/guomaoqiu/myblog_backup/master/uploads/2015/10/Screenshot-from-2015-10-23-140321.png" alt="Screenshot from 2015-10-23 14:03:21"></a> zabbix_server端能获取，现在在zabbix dashboard添加这个item吧 Configuration–&gt;Host–&gt;database-node2–&gt;Items–&gt;Create item <a href="https://raw.githubusercontent.com/guomaoqiu/myblog_backup/master/uploads/2015/10/Screenshot-from-2015-10-23-140754.png" target="_blank" rel="noopener"><img src="https://raw.githubusercontent.com/guomaoqiu/myblog_backup/master/uploads/2015/10/Screenshot-from-2015-10-23-140754.png" alt="Screenshot from 2015-10-23 14:07:54"></a> 这里我们自定义的key 需要手动输入key名称。我这里定义过了，直接点击“Add”就行了 然后我们需要定义一个Triggers Triggers–&gt;Create triggers <a href="https://raw.githubusercontent.com/guomaoqiu/myblog_backup/master/uploads/2015/10/Screenshot-from-2015-10-23-141313.png" target="_blank" rel="noopener"><img src="https://raw.githubusercontent.com/guomaoqiu/myblog_backup/master/uploads/2015/10/Screenshot-from-2015-10-23-141313.png" alt="Screenshot from 2015-10-23 14:13:13"></a> <a href="https://raw.githubusercontent.com/guomaoqiu/myblog_backup/master/uploads/2015/10/Screenshot-from-2015-10-23-141153.png" target="_blank" rel="noopener"><img src="https://raw.githubusercontent.com/guomaoqiu/myblog_backup/master/uploads/2015/10/Screenshot-from-2015-10-23-141153.png" alt="Screenshot from 2015-10-23 14:11:53"></a> 检测它最后一次的取值是不是小于2，定义N的值为2，如果取得的值小于2就说明有问题啦， 定义一下报警： Configuration–&gt;Action–&gt;Create action <a href="https://raw.githubusercontent.com/guomaoqiu/myblog_backup/master/uploads/2015/10/Screenshot-from-2015-10-23-141621.png" target="_blank" rel="noopener"><img src="https://raw.githubusercontent.com/guomaoqiu/myblog_backup/master/uploads/2015/10/Screenshot-from-2015-10-23-141621.png" alt="Screenshot from 2015-10-23 14:16:21"></a></p>
<p>邮件通知内容：<br>MySQL.Repliaction<br>ERROR—MySQL master-slave –&gt;{ITEM.VALUE1}<br>MySQL 主从出现问题，请检测主从状态！！！<br>告警主机 : {HOSTNAME1}<br>告警时间 : {EVENT.DATE} {EVENT.TIME}<br>告警等级 : {TRIGGER.SEVERITY}<br>告警信息 : {TRIGGER.NAME}<br>告警项目 : {TRIGGER.KEY1}<br>问题详情 : {ITEM.NAME}:{ITEM.VALUE}<br>当前状态 : {TRIGGER.STATUS}:{ITEM.VALUE1}<br>事件ID : {EVENT.ID}</p>
<p>恢复后的回复:<br>OK—MySQL master-slave –&gt;{ITEM.VALUE1}<br>MySQL 主从问题恢复，请确认主从状态！！！<br>告警主机 : {HOSTNAME1}<br>告警时间 : {EVENT.DATE} {EVENT.TIME}<br>告警等级 : {TRIGGER.SEVERITY}<br>告警信息 : {TRIGGER.NAME}<br>告警项目 : {TRIGGER.KEY1}<br>问题详情 : {ITEM.NAME}:{ITEM.VALUE}<br>当前状态 : {TRIGGER.STATUS}:{ITEM.VALUE1}<br>事件ID   : {EVENT.ID}</p>
<p>条件： <a href="https://raw.githubusercontent.com/guomaoqiu/myblog_backup/master/uploads/2015/10/Screenshot-from-2015-10-23-141903.png" target="_blank" rel="noopener"><img src="https://raw.githubusercontent.com/guomaoqiu/myblog_backup/master/uploads/2015/10/Screenshot-from-2015-10-23-141903.png" alt="Screenshot from 2015-10-23 14:19:03"></a> <a href="https://raw.githubusercontent.com/guomaoqiu/myblog_backup/master/uploads/2015/10/Screenshot-from-2015-10-23-141935.png" target="_blank" rel="noopener"><img src="https://raw.githubusercontent.com/guomaoqiu/myblog_backup/master/uploads/2015/10/Screenshot-from-2015-10-23-141935.png" alt="Screenshot from 2015-10-23 14:19:35"></a> zabbix自定义脚本发送邮件； 首先我们要在zabbix_server端启用自定义脚本发送邮件，在zabbix_server主配置文件中修改为 AlertScriptsPath=/usr/lib/zabbix/alertscripts  脚本存放的目录 然后我们自定义一个脚本mail.py</p>
<p>#!/usr/bin/python</p>
<p>#coding:utf-8 </p>
<p>import smtplib<br>from email.mime.text import MIMEText<br>import sys </p>
<p>#邮箱服务器地址<br>mail_host = ‘smtp.qq.com’</p>
<p>#邮箱用户名<br>mail_user = ‘xxxxxxx’</p>
<p>#邮箱密码<br>mail_pass = ‘xxxxxxxx’<br>mail_postfix = ‘qq.com’</p>
<p>def send_mail(to_list,subject,content):<br>    me = mail_user+”&lt;”+mail_user+”@”+mail_postfix+”&gt;”<br>    msg = MIMEText(content,_charset=’utf-8’)<br>    msg[‘Subject’] = subject<br>    msg[‘From’] = me<br>    msg[‘to’] = to_list </p>
<pre><code>try:
    s = smtplib.SMTP()
    s.connect(mail_host)
    s.login(mail\_user,mail\_pass)
    s.sendmail(me,to\_list,msg.as\_string())
    s.close()
    return True
except Exception,e:
    print str(e)
    return False
</code></pre><p>if __name__ == “__main__“:<br>    send_mail(sys.argv[1], sys.argv[2], sys.argv[3])</p>
<p>记得加上执行权限； 然后在zabbix dashboard上面定义用户的media加上用户的邮箱即可 <a href="https://raw.githubusercontent.com/guomaoqiu/myblog_backup/master/uploads/2015/10/Screenshot-from-2015-10-23-142528.png" target="_blank" rel="noopener"><img src="https://raw.githubusercontent.com/guomaoqiu/myblog_backup/master/uploads/2015/10/Screenshot-from-2015-10-23-142528.png" alt="Screenshot from 2015-10-23 14:25:28"></a> 以上就可以实现报警啦 测试一下，将mysql-replication.sh这个脚本的值自定义一个输出值为0或者1 <a href="https://raw.githubusercontent.com/guomaoqiu/myblog_backup/master/uploads/2015/10/Screenshot-from-2015-10-23-142832.png" target="_blank" rel="noopener"><img src="https://raw.githubusercontent.com/guomaoqiu/myblog_backup/master/uploads/2015/10/Screenshot-from-2015-10-23-142832.png" alt="Screenshot from 2015-10-23 14:28:32"></a> 查看邮件： <a href="https://raw.githubusercontent.com/guomaoqiu/myblog_backup/master/uploads/2015/10/zabbix_1.png" target="_blank" rel="noopener"><img src="https://raw.githubusercontent.com/guomaoqiu/myblog_backup/master/uploads/2015/10/zabbix_1.png" alt="zabbix_1"></a> 如果恢复后的回复 <a href="https://raw.githubusercontent.com/guomaoqiu/myblog_backup/master/uploads/2015/10/zabbix_2.png" target="_blank" rel="noopener"><img src="https://raw.githubusercontent.com/guomaoqiu/myblog_backup/master/uploads/2015/10/zabbix_2.png" alt="zabbix_2"></a> ok 已经成功能看到这个监控没问题啦。 下面来看一下zabbix + grafana ,这个是作为页面展示比较华丽的效果 <a href="https://raw.githubusercontent.com/guomaoqiu/myblog_backup/master/uploads/2015/10/Screenshot-from-2015-10-23-144225.png" target="_blank" rel="noopener"><img src="https://raw.githubusercontent.com/guomaoqiu/myblog_backup/master/uploads/2015/10/Screenshot-from-2015-10-23-144225.png" alt="Screenshot from 2015-10-23 14:42:25"></a> <a href="https://raw.githubusercontent.com/guomaoqiu/myblog_backup/master/uploads/2015/10/Screenshot-from-2015-10-23-144008.png" target="_blank" rel="noopener"><img src="https://raw.githubusercontent.com/guomaoqiu/myblog_backup/master/uploads/2015/10/Screenshot-from-2015-10-23-144008.png" alt="Screenshot from 2015-10-23 14:40:08"></a> 只要是在zabbix 中有的 items都可以在grafana中展示。</p>

      
    </div>
    
    
    
<div>
  
    <div>
    
        <div style="text-align:center;color: #ccc;font-size:14px;">-------------本文结束<i class="fa fa-paw"></i>感谢您的阅读-------------</div>
    
</div>


  
</div>
    

    

    

    <footer class="post-footer">
      
        <div class="post-tags">
          
            <a href="../../../../tags/zabbix/" rel="tag"># zabbix</a>
          
            <a href="../../../../tags/grafana/" rel="tag"># grafana</a>
          
        </div>
      

      
      
      

      
        <div class="post-nav">
          <div class="post-nav-next post-nav-item">
            
              <a href="../../16/e6-a3-80-e6-9f-a5-e6-96-87-e4-bb-b6-e6-97-b6-e9-97-b4-e6-88-b3-ef-bc-8c-e5-af-b9-e6-af-94-e6-97-b6-e9-97-b4-e7-9a-84shell-e8-84-9a-e6-9c-ac/" rel="next" title="检查文件时间戳，对比时间的Shell脚本">
                <i class="fa fa-chevron-left"></i> 检查文件时间戳，对比时间的Shell脚本
              </a>
            
          </div>

          <span class="post-nav-divider"></span>

          <div class="post-nav-prev post-nav-item">
            
              <a href="../e8-bf-90-e7-bb-b4-e4-ba-ba-e4-bd-a0-e5-ba-94-e8-af-a5-e4-ba-86-e8-a7-a3-e7-9a-84-e4-b8-89-e5-bc-a0-e6-ad-a6-e5-8a-9f-e5-bf-83-e6-b3-95-e5-9b-be-e8-bd-ac-e8-bd-bd/" rel="prev" title="运维人,你应该了解的三张武功心法图(转载)">
                运维人,你应该了解的三张武功心法图(转载) <i class="fa fa-chevron-right"></i>
              </a>
            
          </div>
        </div>
      

      
      
    </footer>
  </div>
  
  
  
  </article>



    <div class="post-spread">
      
    </div>
  </div>


          </div>
          


          

  



        </div>
        
          
  
  <div class="sidebar-toggle">
    <div class="sidebar-toggle-line-wrap">
      <span class="sidebar-toggle-line sidebar-toggle-line-first"></span>
      <span class="sidebar-toggle-line sidebar-toggle-line-middle"></span>
      <span class="sidebar-toggle-line sidebar-toggle-line-last"></span>
    </div>
  </div>

  <aside id="sidebar" class="sidebar">
    
    <div class="sidebar-inner">

      

      

      <section class="site-overview-wrap sidebar-panel sidebar-panel-active">
        <div class="site-overview">
          <div class="site-author motion-element" itemprop="author" itemscope="" itemtype="http://schema.org/Person">
            
              <img class="site-author-image" itemprop="image" src="https://raw.githubusercontent.com/guomaoqiu/myblog_backup/master/uploads/myphoto.png" alt="OutMan">
            
              <p class="site-author-name" itemprop="name">OutMan</p>
              <p class="site-description motion-element" itemprop="description">😊</p>
          </div>

          <nav class="site-state motion-element">

            
              <div class="site-state-item site-state-posts">
              
                <a href="../../../../archives/">
              
                  <span class="site-state-item-count">79</span>
                  <span class="site-state-item-name">日志</span>
                </a>
              </div>
            

            
              
              
              <div class="site-state-item site-state-categories">
                <a href="../../../../categories/index.html">
                  <span class="site-state-item-count">22</span>
                  <span class="site-state-item-name">分类</span>
                </a>
              </div>
            

            
              
              
              <div class="site-state-item site-state-tags">
                <a href="../../../../tags/index.html">
                  <span class="site-state-item-count">46</span>
                  <span class="site-state-item-name">标签</span>
                </a>
              </div>
            

          </nav>

          

          
            <div class="links-of-author motion-element">
                
                  <span class="links-of-author-item">
                    <a href="https://github.com/guomaoqiu" target="_blank" title="GitHub">
                      
                        <i class="fa fa-fw fa-github"></i></a>
                  </span>
                
                  <span class="links-of-author-item">
                    <a href="mailto:guomaoqiu@google.com" target="_blank" title="E-Mail">
                      
                        <i class="fa fa-fw fa-envelope"></i></a>
                  </span>
                
            </div>
          

          
          

          
          

          

        </div>
      </section>

      

      

    </div>
  </aside>


        
      </div>
    </main>

    <footer id="footer" class="footer">
      <div class="footer-inner">
        <div class="copyright">&copy; <span itemprop="copyrightYear">2018</span>
  <span class="with-love">
    <i class="fa fa-user"></i>
  </span>
  <span class="author" itemprop="copyrightHolder">OutMan</span>

  
</div>


  <div class="powered-by">由 <a class="theme-link" target="_blank" href="https://hexo.io">Hexo</a> 强力驱动</div>



  <span class="post-meta-divider">|</span>



  <div class="theme-info">主题 &mdash; <a class="theme-link" target="_blank" href="https://github.com/iissnan/hexo-theme-next">NexT.Gemini</a> v5.1.4</div>




        







        
      </div>
    </footer>

    
      <div class="back-to-top">
        <i class="fa fa-arrow-up"></i>
        
      </div>
    

    

  </div>

  

<script type="text/javascript">
  if (Object.prototype.toString.call(window.Promise) !== '[object Function]') {
    window.Promise = null;
  }
</script>









  


  











  
  
    <script type="text/javascript" src="../../../../lib/jquery/index.js?v=2.1.3"></script>
  

  
  
    <script type="text/javascript" src="../../../../lib/fastclick/lib/fastclick.min.js?v=1.0.6"></script>
  

  
  
    <script type="text/javascript" src="../../../../lib/jquery_lazyload/jquery.lazyload.js?v=1.9.7"></script>
  

  
  
    <script type="text/javascript" src="../../../../lib/velocity/velocity.min.js?v=1.2.1"></script>
  

  
  
    <script type="text/javascript" src="../../../../lib/velocity/velocity.ui.min.js?v=1.2.1"></script>
  

  
  
    <script type="text/javascript" src="../../../../lib/fancybox/source/jquery.fancybox.pack.js?v=2.1.5"></script>
  

  
  
    <script type="text/javascript" src="../../../../lib/canvas-nest/canvas-nest.min.js"></script>
  


  


  <script type="text/javascript" src="../../../../js/src/utils.js?v=5.1.4"></script>

  <script type="text/javascript" src="../../../../js/src/motion.js?v=5.1.4"></script>



  
  


  <script type="text/javascript" src="../../../../js/src/affix.js?v=5.1.4"></script>

  <script type="text/javascript" src="../../../../js/src/schemes/pisces.js?v=5.1.4"></script>



  
  <script type="text/javascript" src="../../../../js/src/scrollspy.js?v=5.1.4"></script>
<script type="text/javascript" src="../../../../js/src/post-details.js?v=5.1.4"></script>



  


  <script type="text/javascript" src="../../../../js/src/bootstrap.js?v=5.1.4"></script>



  


  




	





  





  












  





  

  

  

  
  

  

  

  

</body>
</html>
